//
// Created by Jisam on 2024/8/8 13:47.
// 14点10分 AC
// solve P1596 [USACO10OCT] Lake Counting S
#include <bits/stdc++.h>

#define PSI pair<string,int>
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define VVI vector<vector<int>>
#define VI vector<int>
#define VS vector<string>

#define PQLI priority_queue<int, vector<int>, less<int>>
#define PQGI priority_queue<int, vector<int>, greater<int>>
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

using namespace std;
i64 ans = 1;

int dx[] = {1,1,-1,-1,0,0,-1,1,};
int dy[] = {1,-1,1,-1,1,-1,0,0,};

void bfs(int i,int j,int n,int m ,vector<vector<int>> & vis ,vector<string> & a){
    queue<PII> q;
    q.push({i,j});
    vis[i][j] = ans;
    while(!q.empty()){
        auto [x,y] = q.front();
        q.pop();
//        cout << "f ; " << x << " \n";

        for(int k = 0 ;k < 8 ; k ++)
        {
            int tx = x + dx[k];
            int ty = y + dy[k];

            if(tx >= 0 && ty >= 0 && tx < n && ty < m && vis[tx][ty] == 0 && a[tx][ty] =='W'){
//                cout << tx << " " << ty  <<" \n";
                q.push({tx,ty});
                vis[tx][ty] = vis[x][y];
            }

        }
    }

    ans ++;
}
void solve() {
    int n,m;
    cin >> n >> m;
    vector<string> a(n);
    vector vis(n,vector<int>(m,0));
    for(int i = 0 ; i < n ; i ++)
    {
        cin >> a[i];
    }
    for(int i = 0 ; i < n ; i ++){
        for(int j = 0 ; j < m ; j ++)
        {
            if(vis[i][j] == 0 && a[i][j] == 'W')
            {
                bfs(i,j,n,m,vis,a);
            }
        }
    }

//    for(int i = 0 ; i < n ; i ++){
//        for(int j = 0 ; j < m ; j ++)
//        {
//            cout << vis[i][j] << " ";
//        }
//        cout << "\n";
//    }
    cout << ans - 1;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
//    cin >> T;
    while (T--) solve();
//    cout << "\n"[];
    return 0;
}